package com.jdcloud.csa.utils

import android.text.TextUtils
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 时间格式化工具类
 *
 * @author : shaoshuai27
 * @date ：2020/1/13
 */
object TimeConverterUtils {
    const val UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"
    const val DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"
    const val DATE_FORMAT = "yyyy-MM-dd"
    const val TIME_FORMAT = "HH:mm:ss"
    const val DAY_MILLIS = 1000 * 60 * 60 * 24 // 一天的毫秒值
    const val HOUR_MILLIS = 1000 * 60 * 60 // 一小时的毫秒值

    /**
     * 获取当前时间的指定格式
     *
     * @param timePatten    时间格式 - "yyyy-MM-dd HH:mm:ss"
     * @return "2020-04-30 08:59:17"
     */
    fun getCurrentTime(timePatten: String = DATE_TIME_FORMAT): String {
        val local = SimpleDateFormat(timePatten, Locale.CHINA)
        local.timeZone = TimeZone.getDefault()
        return local.format(Date())
    }

    /**
     * 获取当前时间的UTC格式
     *
     * @return 2020-05-11T04:22:42Z
     */
    fun getCurrentTimeUTC(): String = local2utc(Date())

    // 2020-04-24T09:48:14Z  --> 2020-04-24
    fun utc2LocalShort(utcTime: String?): String? {
        return if (!TextUtils.isEmpty(utcTime)) {
            utc2Local(utcTime!!, UTC_FORMAT, DATE_FORMAT)
        } else null
    }

    // 2020-05-07T08:17:01Z   -->   2020-05-07 16:17:01
    fun utc2Local(utcTime: String?): String? {
        return if (!TextUtils.isEmpty(utcTime)) {
            utc2Local(utcTime!!, UTC_FORMAT, DATE_TIME_FORMAT)
        } else null
    }

    /**
     * UTC时间转本地时间格式
     *
     * @param utcTime           UTC时间 - 2020-04-24T09:48:14Z
     * @param utcTimePatten     UTC时间格式 - "yyyy-MM-dd'T'HH:mm:ss'Z'"
     * @param localTimePatten   本地时间格式 - "yyyy-MM-dd HH:mm:ss"
     *
     * @return 本地时间格式的时间
     * eg:utc2Local("2020-04-24T09:48:14Z", "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd HH:mm:ss")
     */
    fun utc2Local(utcTime: String, utcTimePatten: String, localTimePatten: String): String {
        val utc = SimpleDateFormat(utcTimePatten, Locale.CHINA)
        utc.timeZone = TimeZone.getTimeZone("UTC")//时区定义并进行时间获取
        val utcDate = utc.parse(utcTime)

        val local = SimpleDateFormat(localTimePatten, Locale.CHINA)
        local.timeZone = TimeZone.getDefault()
        val localTime = local.format(utcDate.time)
        return localTime
    }

    /**
     * 获取 UTC 时间
     */
    fun local2utc(date: Date): String {
        val utc = SimpleDateFormat(UTC_FORMAT, Locale.CHINA)
        utc.timeZone = TimeZone.getTimeZone("UTC")//时区定义并进行时间获取
        return utc.format(date)
    }

    /**
     * 计算未来时间 距离 今天的 间隔天数
     */
    fun calculateTimeDifferenceDays(futureDate: String?): Int {
        val diffMillis = calculateTimeDifferenceMillis(futureDate)
        return if (diffMillis != 0L) {
            (diffMillis / DAY_MILLIS).toInt()
        } else 0
    }

    /**
     * 计算未来时间 距离 当前时间的 毫秒值
     *
     * @param futureDate    未来时间格式："yyyy-MM-dd HH:mm:ss"
     * @return 间隔毫秒值
     */
    fun calculateTimeDifferenceMillis(futureDate: String?): Long {
        if (futureDate.isNullOrEmpty())
            return 0L
        try {
            val date = SimpleDateFormat(DATE_TIME_FORMAT, Locale.CHINA).parse(futureDate)
            return if (date != null) {
                val diffMillis: Long = date.time - System.currentTimeMillis()
                diffMillis
            } else 0L
        } catch (e: ParseException) {
        }
        return 0L
    }

    /**
     * 将时间转换为long类型的时间戳
     *
     * @param dateString   时间格式："yyyy-MM-dd HH:mm:ss"
     * @return 对应long类型的时间戳
     */
    fun dateToTimeStamp(dateString: String?): Long {
        if (dateString.isNullOrEmpty()) {
            return 0L
        }
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        var timeStamp = 0L
        try {
            val date = simpleDateFormat.parse(dateString)
            timeStamp = date!!.time
        } catch (e: Exception) {
        }
        return timeStamp
    }

    /** 毫秒转字符串 如：3：55、1：30：30 */
    fun intToString(millisecond: Int): String {
        val dateFormat = SimpleDateFormat("mm:ss", Locale.CHINA)
        return dateFormat.format(millisecond)
    }

}
